package prj3.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

/*
 * SunView.java
 *
 * Created on April 16, 2007, 5:02 PM
 */

/**
 *
 * @author  cwhalen
 */
public class SunView extends javax.swing.JPanel
{

	private BufferedImage earth;
	private BufferedImage sun;

	private double rad_earthRotation = 90;

	private int sun_x;
	private int sun_y;

	private double rad_earthAngle = 0;
	private double earth_distFromSun = 500;

	private int []stars_x;
	private int []stars_y;
	/**
	 * Constructor
	 */
	public SunView()
	{
		initComponents();
		initStarField(50);
		
		this.sun_x = 125;
		this.sun_y = 250;
		
		this.earth = loadImage("earth.png");
		this.sun = loadImage("sun.png");
	}

	private void initStarField(int num) 
	{
		stars_x = new int[num];
		stars_y = new int[num];
		for (int i = 0; i < num; i++)
		{
			stars_x[i] = (int) Math.round(Math.random() * 250);
			stars_y[i] = (int) Math.round(Math.random() * 380);
		}
	}

	/**
	 * Loads and returns a BufferedImage
	 * 
	 * @param path file path
	 * @return
	 */
	private BufferedImage loadImage(String path)
	{
        BufferedImage image = null;
        URL imgURL = SunView.class.getResource(path);
        try {
        	image = ImageIO.read(imgURL);
        } catch(IOException e) {
            System.out.println("IO error: " + e.getMessage());
        }
        return image;
	}


	public void updateView(double rotation, double earthAngle, double distFromSun)
	{
		this.rad_earthRotation = rotation;
		this.rad_earthAngle = earthAngle;
		this.earth_distFromSun = distFromSun;
		
		this.repaint();
	}


	/**
	 * @param g2
	 */
	private void drawStars(Graphics2D g2)
	{
		g2.setColor(Color.WHITE);
		for (int i = 0; i < 50; i++)
		{
			g2.drawLine(stars_x[i], stars_y[i], stars_x[i], stars_y[i]);
		}

	}
	
	boolean flip = false;
	
	double p1 = 0;
	double p2 = 0;
	double p3 = 0;
	
	private void drawCelestBodies(Graphics2D g2)
	{
		//Draw Sun
		double sun_centerX = (sun.getRaster().getWidth() - sun.getRaster().getMinX()) / 2.0;
		double sun_centerY = (sun.getRaster().getHeight() - sun.getRaster().getMinY()) / 2.0;
		
		g2.drawImage(sun, (int)(sun_x-sun_centerX), (int)(sun_y-sun_centerY), this);
		
		//draw Earth
		double deg_earthAngle = Math.toDegrees(this.rad_earthAngle);
	
		double earth_x;
		double earth_y;
		
		double earth_centerX;
		double earth_centerY;
		
		if(this.rad_earthAngle > ((p1+p2+p3)/3))
		{
			earth_x = sun_x + (Math.sin(this.rad_earthAngle)* (this.earth_distFromSun*.75));
			earth_y = sun_y + (Math.cos(this.rad_earthAngle)* (this.earth_distFromSun*.75));
			
			earth_centerX = (earth.getRaster().getWidth() - earth.getRaster().getMinX()) / 2.0;
			earth_centerY = (earth.getRaster().getHeight() - earth.getRaster().getMinY()) / 2.0;
			
			AffineTransform atx = AffineTransform.getRotateInstance(-rad_earthRotation, earth_centerX, earth_centerY);
			AffineTransformOp op = new AffineTransformOp(atx, AffineTransformOp.TYPE_BILINEAR);

			g2.drawImage(op.filter(earth, null), null, (int)(earth_x-earth_centerX), (int)(earth_y-earth_centerY));
			
		} else
		{
			earth_x = sun_x - (Math.sin(this.rad_earthAngle)* (this.earth_distFromSun*.75));
			earth_y = sun_y + (Math.cos(this.rad_earthAngle)* (this.earth_distFromSun*.75));
			
			earth_centerX = (earth.getRaster().getWidth() - earth.getRaster().getMinX()) / 2.0;
			earth_centerY = (earth.getRaster().getHeight() - earth.getRaster().getMinY()) / 2.0;
			
			AffineTransform atx = AffineTransform.getRotateInstance(rad_earthRotation, earth_centerX, earth_centerY);
			AffineTransformOp op = new AffineTransformOp(atx, AffineTransformOp.TYPE_BILINEAR);

			g2.drawImage(op.filter(earth, null), null, (int)(earth_x-earth_centerX), (int)(earth_y-earth_centerY));
		}
		p3 = p2;
		p2 = p1;
		p1 = this.rad_earthAngle;
		
		
		
	}


	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		Graphics2D g2 = (Graphics2D) g;

		this.drawStars(g2);
		this.drawCelestBodies(g2);
	}


	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
	private void initComponents()
	{

		setLayout(new java.awt.BorderLayout());

		setBackground(new java.awt.Color(0, 30, 76));
		setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204,
				204, 204), 5));
		setMaximumSize(new java.awt.Dimension(250, 300));
		setMinimumSize(new java.awt.Dimension(250, 300));
		setPreferredSize(new java.awt.Dimension(250, 300));
	}
	// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	// End of variables declaration//GEN-END:variables

}
